package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Address;
import com.sansi.pencilbook.mapper.AddressMapper;
import com.sansi.pencilbook.service.AddressServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 收货地址服务
 *
 * @author lsy13
 */
@Service
public class AddressServerImpl implements AddressServer {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Result getAllAddresses() {
        try {
            List<Address> addresses = addressMapper.findAll();
            return addresses.size()>0 ? Result.ofSuccess(addresses.size(), addresses) :
                    Result.ofFailed("查询地址为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址查询失败");
        }
    }

    @Override
    public Result getAddressesByUser(int userId) {
        try {
            List<Address> addresses = addressMapper.findByUserId(userId);
            return addresses.size()>0 ? Result.ofSuccess(addresses.size(), addresses) :
                    Result.ofFailed("查询地址为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址查询失败");
        }
    }

    @Override
    public Result getAddressByIdAndAddress(int id, String address) {
        try {
            Address addresses = addressMapper.findByUserIdAndAddress(id, address);
            return addresses!=null ? Result.ofSuccess(1, addresses) :
                    Result.ofFailed("查询地址为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址查询失败");
        }
    }

    @Override
    public Result getStoreAddress(int storeId) {
        try {
            Address addresses = addressMapper.findByStoreId(storeId);
            return addresses!=null ? Result.ofSuccess(1, addresses) :
                Result.ofFailed("查询地址为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址查询失败");
        }
    }

    @Override
    public Result addAddress(Address address) {
        try {
            return addressMapper.saveAddress(address)>0 ? Result.ofSuccess(1) :
                    Result.ofFailed("保存地址失败");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址保存失败");
        }
    }

    @Override
    public Result modifyAddress(Address address) {
        try {
            return addressMapper.updateAddress(address)>0 ? Result.ofSuccess(1) :
                    Result.ofFailed("修改地址失败");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址修改失败");
        }
    }

    @Override
    public Result deleteAddress(Address address) {
        try {
            return addressMapper.deleteAddress(address)>0 ? Result.ofSuccess(1) :
                    Result.ofFailed("地址已经删除");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址删除失败");
        }
    }

    @Override
    public Result deleteAddresses(List<Address> addresses) {
        try {
            int num = addressMapper.deleteAddresses(addresses);
            return num > 0 ? Result.ofSuccess("批量删除地址成功", num) :
                    Result.ofFailed("地址已经删除");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("地址删除失败");
        }
    }
}
